$(function() {
    $(document.body).click(function() {
        $(".e").fadeIn();
    });
    $(".expandall a").bind("click", function() {
        expand();
    });
    $(document).bind('keyup', function(e) {
        e = e || window.event;
        var t = e.keyCode;
        //判断是否存在class
        if ($(".g" + t).html() != null) {
            current = $(".g" + t).parent().parent().next(".content");
            if ($(".content:visible").size() == 0) {
                current.slideDown({duration: 800, easing: 'easeOutExpo'});
            } else {
                $(".content:visible").slideUp({duration: 800, easing: 'easeOutExpo'});
                current.slideDown();
            }
            $("ul.menu li a").removeClass("clicked");
            var li = current.find("li").first();
            //window.top.frames['main'].location.href = li.find("a").attr("href");
            navfrm(li.find("a"));
        } else if (t == 27) {
            //ESC
            expand();
        } else if (t == 32) {
            window.top.$("#switch").click();
        }
    });
    //快捷键
    $(document).bind('keydown', function(e) {
        e = e || window.event;
        if (e.keyCode == 27) {
            $(window.parent.frames['left']).focus();
            $(window.parent.frames['left'].document).find(".e").fadeIn();
        } else if (e.keyCode == 113) {
            window.parent.search();
        }
    });
    $(document).bind('keypress', function(e) {
        if (e.ctrlKey && e.which == 13 || e.which == 10) {

        } else if (e.shiftKey && e.which == 13 || e.which == 10) {

        } else if (e.shiftKey && e.which == 90) {
            window.parent.search();
        }
    });
    //展开快捷方式菜单
    function expand() {
        var obj = $(window.parent.document.getElementById("expandall"));
        if (obj.css("display") == "none") {
            obj.slideDown({duration: 800, easing: 'easeOutExpo'});
            if (obj.find("h2").html() == null) {
                obj.css("left", "181px");
                obj.css("top", "50px");
                $("#container ul li a").attr("target", "main");
                $("#container h1").each(function() {
                    obj.append("<div class='menuitem'><h2>" + $(this).html() + "</h2><ul>" + $(this).next().find("ul").html() + "</ul></div>");
                });
                obj.find("a").click(function() {
                    obj.slideUp({duration: 800, easing: 'easeOutExpo'});
                });
            }
        } else {
            obj.slideUp({duration: 800, easing: 'easeOutExpo'});
        }
    }
    //删除焦点信息
    $('a,input[type="button"],input[type="submit"]').bind('focus', function() {
        if (this.blur) {
            this.blur();
        }
    });
    var text = SG.tools.cookie("navtext");
    var href = SG.tools.cookie("navhref");
    if (text && href && text != LANG.HOME) {
        $("ul.menu li a[rel='" + text + "']").closest(".content").slideDown();
    } else {
        $(".content").eq(0).slideDown({duration: 800, easing: 'easeOutBounce'});
    }
    //点击菜单导航
    $("h1").click(function() {
        var current = $(this).next(".content");
        if (current.css("display") == "block") {
            current.slideUp({duration: 800, easing: 'easeOutExpo'});
        } else {
            var n = $(this).next().find("ul>li:first a");
            if ($(".content:visible").size() == 0) {
                current.slideDown({duration: 800, easing: 'easeOutExpo'}, function() {
                    n.click();
                });
            } else {
                $(".content:visible").slideUp({duration: 800, easing: 'easeOutExpo'});
                current.slideDown('fast', function() {
                    n.click();
                });
            }
        }
    });
    $("ul.menu li a:not([target])").click(function() {
        navfrm(this);
        return false;
    });

    function navfrm(obj) {
        window.parent.navadd($(obj).text(), $(obj).attr("href"));
        return false;
    }
});